import initProviders from './initProviders';
import initCornerstoneDICOMImageLoader from './initCornerstoneDICOMImageLoader';
import initVolumeLoader from './initVolumeLoader';
import { init as csToolsInit } from '@roos/cornerstone3d-tools';
import { init as csRenderInit } from '@cornerstonejs/core';

export default async (modsInStudy:string,config?: any) => {
  initProviders(modsInStudy);
  initCornerstoneDICOMImageLoader();
  initVolumeLoader();
  await csRenderInit({
    peerImport,
    ...(config?.core ? config.core : {})
  });
  await csToolsInit();
};

const peerImport = async (moduleId: string) => {
  if (moduleId === 'dicom-microscopy-viewer') {
    return importGlobal('./dicom-microscopy-viewer/dicomMicroscopyViewer.min.js', 'dicomMicroscopyViewer');
  }
};

const importGlobal = async (path: string, globalName: any) => {
  await import(/* webpackIgnore: true */ path);
  return window[globalName];
};
